#include "Service.h"

#define CRYPTOPP_DISABLE_ASM
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <CryptoPP/crc.h>
#include <CryptoPP/hex.h>

namespace XyNet
{

	Error::Error(const string& name, const string& brief)
		: _name(name), _brief(brief), _codeString(makeId(name)) {}

	Method::Method(const string& name) : _name(name), _id(makeId(name)) {}

	Service::Service(const string& name) : _name(name), _id(makeId(name)) {}

	string makeId(const string& name)
	{
		using namespace CryptoPP;

		if (name == "NotError")
			return "00000000";
		else if (name == "UnknownError")
			return "FFFFFFFF";

		CRC32 crc;
		string digest(crc.DigestSize(), 0);
		crc.CalculateDigest((byte*)digest.data(), (byte*)name.data(), name.length());

		string id;
		StringSource(digest, true, new HexEncoder(new StringSink(id), true));

		if (id == "00000000" || id == "FFFFFFFF")
			throw std::runtime_error("无效id");
		return id; 
	}

}